<template>
  <div class="">
    <div class="sweets-wrapper">
      <div class="top-sweets">
        <div class="top">
          <img src="http://tpl-c913457.pic-tpl.websiteonline.cn/upload/oj7x.png" width="24px" style="transform:rotateY(180deg);"  alt="">
          <span>美食甜品</span>
          <img src="http://tpl-c913457.pic-tpl.websiteonline.cn/upload/oj7x.png" alt="">
        </div>
        <p>热销推荐·较新推荐</p>
        <span class="sort" @click="sortPrice">Sort by: Price</span>
      </div>
      <ul class="checked-li">
        <li
          v-for="(item, index) in nameList"
          :key="index"
          @click="getCheckGoods(item.classtify)"
          :class="{'cur': $route.query.classtify === item.classtify}"
        >
          {{item.classtifyName}}
        </li>
      </ul>
      <div class="block-wrapper">
        <block :productInfo="productInfo"></block>
      </div>
    </div>
    <nav-footer></nav-footer>
  </div>
</template>

<script>
import navFooter from '../public/navFooter'
import block from '../components/block'
import axios from 'axios'
export default {
  components: {
    navFooter,
    block
  },
  data () {
    return {
      productInfo: [],
      classtify: this.$route.query.classtify,
      nameList: [
        {
          classtifyName: '提拉米苏',
          classtify: 'T'
        },
        {
          classtifyName: '布丁',
          classtify: 'B'
        },
        {
          classtifyName: '芝士蛋糕',
          classtify: 'Z'
        },
        {
          classtifyName: '西式甜点',
          classtify: 'X'
        },
        {
          classtifyName: '马卡龙',
          classtify: 'M'
        }
      ]
    }
  },
  mounted () {
    this.getDetailInfo()
  },
  methods: {
    // 从跳转路由里面获取参数，进行商品查询
    getDetailInfo () {
      console.log()
      axios.post('/goods/detailClasstify', {
        classtify: this.classtify
      }).then((res) => {
        if (res.data.status === 0) {
          this.productInfo = res.data.result.list
          console.log(res.data.result.list)
        }
      })
    },

    // 点击进行商品查询
    getCheckGoods (clickCls) {
      let query = this.$router.history.current.query
      let path = this.$router.history.current.path
      let newQuery = JSON.parse(JSON.stringify(query))
      newQuery.classtify = clickCls
      this.$router.push({ path, query: newQuery })
      var param = {
        classtify: clickCls
      }
      axios.get('/goods/checkClasstify', {
        params: param
      }).then((res) => {
        if (res.data.status === 0) {
          this.productInfo = res.data.result.list
          console.log(res.data.result.list)
        }
      })
    },

    // 价格升降序
    sortPrice () {
      let query = this.$router.history.current.query
      let path = this.$router.history.current.path
      let newQuery = JSON.parse(JSON.stringify(query))
      console.log('newQuery', newQuery.classtify)
      this.$router.push({ path, query: newQuery })
      var param = {
        classtify: newQuery.classtify
      }
      axios.get('/goods/goodsSort', {
        params: param
      }).then((res) => {
        if (res.data.status === 0) {
          this.productInfo = res.data.result.list
          console.log(res.data)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.sweets-wrapper {
  margin-bottom: 30px;
  .top-sweets {
    padding-top: 40px;
    .top {
      width: 11%;
      height: 30px;
      display: flex;
      margin: 0 auto;
      margin-bottom: 10px;
      span {
        padding: 20px;
        padding-top: 8px;
        color: #8f6228;
      }
    }
    p {
      text-align: center;
      letter-spacing: 10px;
      color: #857560;
    }
    .sort {
      float: right;
      color: #fff;
      margin-right: 50px;
      padding: 10px;
      background-color: #fab965;
      border: 1px solid #fdef27;
      cursor: pointer;
      border-radius: 8px;
    }
  }
  .checked-li {
    width: 40%;
    margin: 40px auto;
    display: flex;
    li {
      padding: 10px 35px 10px 30px;
      background-color: #bf803e;
      flex-direction: row;
      justify-content: space-between;
      text-align: center;
      color: #fff;
    }
    li:hover {
      background-color: #ac6e2e;
      font-weight: 700;
    }
    .cur {
      background-color: #ac6e2e;
      font-weight: 700;
    }
  }
  .block-wrapper {
    width: 70%;
    margin-left: 300px;
  }
}
</style>
